package com.myproject.shop.controller;

import com.myproject.shop.domain.Goods;
import com.myproject.shop.service.IGoodsService;
import com.myproject.shop.util.AjaxResult;
import com.myproject.shop.util.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/goods")
@Api(tags = "商品管理825王慧敏")
public class GoodsController {





        @Autowired
        private IGoodsService goodsService;

        @ApiOperation("获得全部商品列表825王慧敏")
        @GetMapping("/listAll")
        public AjaxResult getAllGoodsList() {
            return AjaxResult.success(goodsService.selectAllGoods());
        }

        @ApiOperation("根据商品名称查询商品列表825王慧敏")
        @ApiImplicitParam(name = "name", value = "商品名称",
                dataType = "string", dataTypeClass = String.class)
        @GetMapping("/list")
        public AjaxResult list(@RequestParam(value = "name", required = false) String goodsName) {
            List<Goods> list = goodsService.selectGoodsByParam(goodsName);
            return AjaxResult.success(list);
        }
    @ApiOperation("根据id获取会员详细信息825王慧敏")
    @ApiImplicitParam(name = "id", value = "商品编号（ID）",
            required = true, dataType = "integer", paramType = "path",
            dataTypeClass = Integer.class)
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer gid) {
        Goods goods = goodsService.selectGoodsById(gid);
        if (goods == null || goods.getGid() == null) {
            throw new BizException(500, "会员 " + gid + " 不存在");
        }
        return AjaxResult.success(goods);
    }

        @ApiOperation("新增商品825王慧敏")
        @PostMapping
        public AjaxResult add(@RequestBody Goods goods) {
            return AjaxResult.success(goodsService.insertGoods(goods));
        }

        @ApiOperation("修改商品825王慧敏")
        @PutMapping
        public AjaxResult edit(@RequestBody Goods goods) {
            return AjaxResult.success(goodsService.updateGoods(goods));
        }

        @ApiOperation("删除商品825王慧敏")
        @ApiImplicitParam(name = "id", value = "商品编号（ID）", required = true, dataType = "integer", paramType = "path", dataTypeClass = Integer.class)
        @DeleteMapping("/{id}")
        public AjaxResult remove(@PathVariable Integer id) {
            return AjaxResult.success(goodsService.deleteGoodsById(id));
        }

}
